﻿/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.chasekernan.hxnova.componenteditor;

import com.chasekernan.hxnova.core.races.LRTSet;
import com.chasekernan.hxnova.core.races.PRTSet;
import org.aswing.border.EmptyBorder;
import org.aswing.ButtonGroup;
import org.aswing.ext.Form;
import org.aswing.Insets;
import org.aswing.JCheckBox;
import org.aswing.JFrame;
import org.aswing.JOptionPane;
import org.aswing.JPanel;
import org.aswing.JRadioButton;
import org.aswing.plaf.basic.icon.RadioButtonIcon;
import org.aswing.JButton;
import com.chasekernan.hxnova.core.components.Component;
import flash.events.Event;
import org.aswing.geom.IntDimension;

class RaceAvailabilityWindow extends JFrame {
    
    private var lrts : Hash<JCheckBox>;
    private var prts : Hash<JCheckBox>;
    private var anyLRT : JRadioButton;
    private var notLRT : JRadioButton;
    private var onlyLRT : JRadioButton;
    private var lrtGroup : ButtonGroup;
    
    private var okButton : JButton;
    private var cancelButton : JButton;
    
    public function new(?parent : Dynamic) {
        super(parent, "Race Availability", true);
        
        setContentPane(setupContents());
        //setSize(new IntDimension(370, 165));
        //setPreferredSize(new IntDimension(370, 165));
        setLocationXY(100, 225);
        pack();
        
        okButton.addActionListener(onOK);
        cancelButton.addActionListener(onCancel);
        
        anyLRT.addActionListener(onLRTOptionClicked);
        notLRT.addActionListener(onLRTOptionClicked);
        onlyLRT.addActionListener(onLRTOptionClicked);
        
        loadFromComponent();
    }
    
    private function setupContents() : Form {
        var form = new Form();
        form.setBorder(new EmptyBorder(null, new Insets(5, 5, 5, 5)));
        
        form.append(form.createRow([form.createLeftLabel("PRT availability:"), null, null]));
        
        prts = new Hash();
        var currentRow = new Array<Dynamic>();
        
        for (prt in PRTSet.PRT_LIST) {
            var checkBox = new JCheckBox(Std.string(prt));
            checkBox.setSelected(true);
            prts.set(Std.string(prt), checkBox);
            currentRow.push(checkBox);
            
            if (currentRow.length >= 3) {
                form.append(form.createRow(currentRow));
                currentRow = new Array();
            }
        }
        if (currentRow.length != 0) form.append(form.createRow(currentRow));
        currentRow = new Array();
        
        form.addSeparator();
        
        form.append(form.createRow([form.createLeftLabel("LRT availability:"), null, null]));
        
        anyLRT = new JRadioButton("Any LRT");
        anyLRT.setSelected(true);
        
        onlyLRT = new JRadioButton("Only the following:");
        onlyLRT.setSelected(false);
        
        notLRT = new JRadioButton("Not the following:");
        notLRT.setSelected(false);
        
        lrtGroup = new ButtonGroup();
        lrtGroup.append(anyLRT);
        lrtGroup.append(onlyLRT);
        lrtGroup.append(notLRT);
        
        form.append(form.createRow([anyLRT, onlyLRT, notLRT]));
        
        lrts = new Hash();
        for (lrt in LRTSet.LRT_LIST) {
            var checkBox = new JCheckBox(Std.string(lrt));
            checkBox.setSelected(true);
            lrts.set(Std.string(lrt), checkBox);
            currentRow.push(checkBox);
            
            if (currentRow.length >= 3) {
                form.append(form.createRow(currentRow));
                currentRow = new Array();
            }
        }
        if (currentRow.length != 0) form.append(form.createRow(currentRow));
        
        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");
        form.append(form.createRow([okButton, null, cancelButton]));
        
        return form;
    }
    
    private function loadFromComponent() {
        var component = ComponentEditorPanel.INSTANCE.editedComponent;
        if (component == null) return;
        
        //set all prts unselected
        for (prtCheckBox in prts) prtCheckBox.setSelected(false);
        
        for (prt in component.availableToPRTs) {
            prts.get(Std.string(prt)).setSelected(true);
        }
        
        switch(component.lrts) {
            
            case Any: 
                anyLRT.setSelected(true);
                setLRTsEnabled(false);
            
            case Not(lrtArray):
                setLRTsChecked(lrtArray);
                notLRT.setSelected(true);
                setLRTsEnabled(true);
                
            case Only(lrtArray):
                setLRTsChecked(lrtArray);
                onlyLRT.setSelected(true);
                setLRTsEnabled(true);
                
        };
    }
    
    //returns true if successful
    private function saveToComponent() : Bool {
        var component = ComponentEditorPanel.INSTANCE.editedComponent;
        if (component == null) return false;
        
        var prtsSelected = new Array<PRT>();
        for (prt in PRTSet.PRT_LIST) {
            if (prts.get(Std.string(prt)).isSelected()) prtsSelected.push(prt);
        }
        component.availableToPRTs = prtsSelected;
        
        var lrtsSelected = new Array<LRT>();
        for (lrt in LRTSet.LRT_LIST) {
            if (lrts.get(Std.string(lrt)).isSelected()) lrtsSelected.push(lrt);
        }
        
        var lrts = untyped switch(lrtGroup.getSelectedButton()) {
            
            case anyLRT: Any;
            
            case notLRT: 
                if (lrtsSelected.length == 0) {
                    JOptionPane.showMessageDialog("Error", "The 'Not' option requires at least one lrt to be selected.");
                    null;
                } else Not(lrtsSelected);
            
            case onlyLRT: 
                if (lrtsSelected.length == 0) {
                    JOptionPane.showMessageDialog("Error", "The 'Only' option requires at least one lrt to be selected.");
                    null;
                } else Only(lrtsSelected);
        }
		
		if (lrts == null) return false;
		component.lrts = lrts;
        
        return true;
    }
    
    private function setLRTsChecked(lrtArray : Array < LRT > ) {
        for (lrtCheck in lrts) lrtCheck.setSelected(false);
        
        for (lrt in lrtArray) {
            lrts.get(Std.string(lrt)).setSelected(true);
        }
    }
    
    private function onLRTOptionClicked(?e : Dynamic) {
        setLRTsEnabled(!anyLRT.isSelected());
    }
    
    private function setLRTsEnabled(enabled : Bool) {
        for (lrt in LRTSet.LRT_LIST) {
            lrts.get(Std.string(lrt)).setEnabled(enabled);
        }
    }
    
    private function onOK(?e : Dynamic) {
        if(saveToComponent()) tryToClose();
    }
    
    private function onCancel(?e : Dynamic) {
        tryToClose();
    }
    
}